package cc.linker.steplink.crm.merchant.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class CheckImage {
    /**
     * 验证图片大小 限制在一M内
     *
     * @param file
     * @return
     */
    @SuppressWarnings("serial")
    public static Map<String, String> imgMap = new HashMap<String, String>() {
        {
            put("image/jpeg", ".jpg");
            put("image/pjpeg", ".jpg");
            put("image/x-png", ".png");
            put("image/png", ".png");
        }
    };

    @SuppressWarnings("resource")
    public static HashMap<String, Object> checkSize(File file) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        FileInputStream ins;
        try {
            ins = new FileInputStream(file);
            //判断文件是否超过一M   
            if (ins.available() > 1024 * 1024 * 1) {
                file.delete();
                map.put("success", false);
                map.put("msg", "上传文件不得超过1M");
            } else {
                map.put("success", true);
                map.put("msg", "");
            }
        } catch (FileNotFoundException e) {
            map.put("success", false);
            map.put("msg", "系统读取图片时出现异常，请联系管理员");
        } catch (IOException e) {

            map.put("success", false);
            map.put("msg", "系统读取图片时出现异常，请联系管理员");
        }
        return map;
    }

    /**
     * @param file
     * @param size
     * @return boolean
     * @throws
     * @Description: 验证图片大小
     * @author liu_wenfeng
     * @date 2015年8月12日 下午1:42:07
     */
    public static boolean validImageSize(MultipartFile file, int size) {
        InputStream ins = null;
        boolean result = false;
        try {
            ins = file.getInputStream();
            //判断文件是否超过 size 单位：kb  
            if (!(ins.available() > size)) {
                result = true;
            }
        } catch (FileNotFoundException e) {
            LOGGER.info("找不到读取文件");
        } catch (IOException e) {
            LOGGER.info("系统读取图片时出现异常，请联系管理员");
        } finally {
            try {
                ins.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * @param file
     * @param width
     * @param height
     * @return boolean
     * @throws
     * @Description: 验证图片尺寸大小
     * @author liu_wenfeng
     * @date 2015年8月12日 下午1:42:31
     */
    public static boolean validImageSize(MultipartFile file, int width, int height) {
        boolean result = false;
        Image src;
        try {
            src = javax.imageio.ImageIO.read(file.getInputStream());
            int w = src.getWidth(null);
            int h = src.getHeight(null);
            if (w <= width && h <= height) {
                result = true;
            }
        } catch (IOException e) {
            e.printStackTrace();
            LOGGER.info("系统读取图片大小出现异常，请联系管理或调整图片");
        }
        return result;
    }

    /**
     * 检测文件长度是否符合
     *
     * @param height
     * @return
     */
    public static HashMap<String, Object> checkImgSize(File file, int width, int height) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        Image src;
        try {
            src = javax.imageio.ImageIO.read(file);
            int w = src.getWidth(null);
            int h = src.getHeight(null);
            if (w <= width && h <= height) {
                map.put("success", true);
                map.put("msg", "true");
            } else {
                map.put("success", false);
                map.put("msg", "文件上传失败，请上传图片宽：" + width + " 高:" + height);
            }
        } catch (IOException e) {

            e.printStackTrace();
            map.put("success", false);
            map.put("msg", "系统读取图片大小出现异常，请联系管理或调整图片");
        }
        return map;
    }


    /**
     * 检测LOGO是否符合
     *
     * @param file
     * @param width
     * @param height
     * @return
     */
    public static HashMap<String, Object> checkImgSizeLoGo(File file, int width, int height) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        Image src;
        try {
            src = javax.imageio.ImageIO.read(file);
            int w = src.getWidth(null);
            int h = src.getHeight(null);
            if (w == width && h == height) {
                map.put("success", true);
                map.put("msg", "true");
            } else {
                map.put("success", false);
                map.put("msg", "文件上传失败，请上传图片宽：" + width + " 高:" + height);
            }
        } catch (IOException e) {

            e.printStackTrace();
            map.put("success", false);
            map.put("msg", "系统读取图片大小出现异常，请联系管理或调整图片");
        }
        return map;
    }

    public static int getImageHeight(MultipartFile file) {
        Image src;
        int h = 0;
        try {
            src = javax.imageio.ImageIO.read(file.getInputStream());
            h = src.getHeight(null);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return h;

    }

    public static int getImageWidth(MultipartFile file) {
        Image src;
        int h = 0;
        try {
            src = javax.imageio.ImageIO.read(file.getInputStream());
            h = src.getWidth(null);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return h;

    }
}
